package com.buding.fx.utils;

import com.buding.fx.common.enums.ButtonTypeEnum;
import com.jfoenix.controls.JFXButton;
import com.jfoenix.svg.SVGGlyph;
import com.jfoenix.svg.SVGGlyphLoader;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author mingdongwei-jwk
 * @date 2022/09/29 10:49
 * @class com.buding.fx.utils
 * @Description
 */
public class ButtonUtils {

    public static void main(String[] args) {
        ButtonUtils.createButton(20,10,ButtonTypeEnum.ADD,"增加列");
    }
    private static  Logger logger = LoggerFactory.getLogger(ButtonUtils.class);

    public static Button createButton(ButtonTypeEnum buttonType,String title){
       return createButton(buttonType.getWidth(),buttonType.getHeight(),buttonType.getFontSize(),buttonType.getColor(),buttonType.getIcon(),title==null?buttonType.getDesc():title,buttonType.getColor());
    }

    public static Button createButton(double wigth,double height,ButtonTypeEnum buttonType,String title){
        return createButton(wigth,height,buttonType.getFontSize(),buttonType.getColor(),buttonType.getIcon(),title==null?buttonType.getDesc():title,buttonType.getColor());
    }

    public static JFXButton createButton(double wigth, double height, ButtonTypeEnum buttonType, String title, int fontSize, int svgSize){
        Set<String> allGlyphsIDs = SVGGlyphLoader.getAllGlyphsIDs();
        Map<Integer, SVGGlyph> svgMap = new HashMap<>();
        for (String allGlyphsID : allGlyphsIDs) {
            SVGGlyph glyph = SVGGlyphLoader.getGlyph(allGlyphsID);
            svgMap.put(glyph.getGlyphId(), glyph);
        }
        SVGGlyph svg = null;
        String color = "#000000";
        Integer svgId = 99;
        switch (buttonType){
           case ADD:
               svgId = 99;
               color = "#010000";
               break;
            case  DELETE:
                svgId = 21;
                color = "#fe0000";
                break;
            case SAVE:
                svgId = 14;
                color = "#4d74e6";
                break;
            case UPDATE:
                svgId = 526;
                color = "#1a7f7f";
                break;
            case EDIT:
                svgId = 526;
                color = "#1a7f7f";
                break;
            case WARNING:
                svgId = 268;
                color = "#b31a1a";
                break;
            case WEIXIAN:
                svgId  = 108;
                color = "#990000";
                break;
            case CLEAR:
                svgId = 465;
                color = "#cc6643";
                break;
            case SEARCH:
                svgId = 606;
                color = "#001a80";
                break;
       }
       svg = svgMap.get(svgId);
        if (svg!=null){
            svg.setSize(svgSize);
            svg.setFill(Color.web(color));
        }
       JFXButton button = new JFXButton(title,svgMap.get(svgId));
       button.setPrefWidth(wigth);
       button.setFont(Font.font(fontSize));
       return button;
    }

    /**
     *
     * @param width 按钮长
     * @param height 宽
     * @param fontSize 字体大小
     * @param color 背景颜色
     * @param icon 图片
     * @param title 文字
     * @param fontColor 文字颜色
     * @return
     */
   public static Button createButton(double width,double height,int fontSize,String color,String icon,String title,String fontColor){
       Button button = new Button(title);
       button.setPrefWidth(width);
       button.setPrefHeight(height);
       logger.info("create button width: "+button.getPrefWidth()+"; height: "+button.getPrefHeight());
       FontWeight fontWeight = FontWeight.NORMAL;
       FontPosture fontPosture = FontPosture.REGULAR;
       String fontFamily = "Droid Serif";
       Font font = Font.font(fontFamily,fontWeight,fontPosture,fontSize);
       button.setFont(font);
       button.setStyle("-fx-background-color: "+fontColor);
       button.setAlignment(Pos.CENTER);
       button.setTextFill(Color.web("#ffffff"));
       CornerRadii radii = new CornerRadii(15);  //圆角度
       Border border = new Border(new BorderStroke(Color.web(color), BorderStrokeStyle.SOLID,radii,new BorderWidths(1,1,1,1)));
       button.setBorder(border);
       Image image = new Image(icon);
       ImageView imageView = new ImageView(image);
       imageView.setFitHeight(15);
       imageView.setFitWidth(15);
       button.setGraphic(imageView);
       button.setGraphicTextGap(5);
       return button;
   }


}
